package com.example.wireless2_spring_boot.controller;

import com.example.wireless2_spring_boot.damain.User;
import com.example.wireless2_spring_boot.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author XiaolongLi
 * Description:
 * @date 2021-10-16 15:15
 */

@Api(tags = {"无线二用户接口"})
@RestController
@RequestMapping("/api")
public class UserController {

    @Resource
    UserService userService;

    /**
     * HelloWorld
     */
    @GetMapping(value = "/hello/{str}")
    public String helloWorld(@PathVariable String str) {
        if(str == null) return "hello";
        return "hello " + str;
    }

    @ApiOperation("添加用户")
    @PostMapping(value = "/insert")
    public String addUser(@RequestBody User user) {
        userService.insert(user);
        return "增加用户成功";
    }

    @ApiOperation(value = "删除用户", notes = "{id} 路径参数")
    @DeleteMapping(value = "/delete/{id}")
    public String deleteUser(@PathVariable("id") Integer id) {
        userService.deleteByPrimaryKey(id);
        return "增加用户成功";
    }

    @ApiOperation(value = "查看所有用户")
    @GetMapping(value = "/select/all")
    public Object selectAll() {
        List<User> users = userService.selectAll();
        return users;
    }

    @ApiOperation(value = "修改用户信息")
    @PostMapping(value = "/modify")
    public String modifyUser(@RequestBody User user) {
        if(userService.updateByPrimaryKeySelective(user) > 0) {
            return "修改用户成功";
        }
        return "修改用户失败";
    }

}
